<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      /*
      思路：定义两个map存放字符出现次数，然后判断大小即可
       */
      var canConstruct = function (ransomNote, magazine) {
        let mapA = new Map()
        for (let i = 0; i < ransomNote.length; i++) {
          if (mapA.get(ransomNote[i])) {
            mapA.set(ransomNote[i], mapA.get(ransomNote[i]) + 1)
          } else {
            mapA.set(ransomNote[i], 1)
          }
        }
        let mapB = new Map()
        for (let i = 0; i < magazine.length; i++) {
          if (mapB.get(magazine[i])) {
            mapB.set(magazine[i], mapB.get(magazine[i]) + 1)
          } else {
            mapB.set(magazine[i], 1)
          }
        }
        for (let i of mapA) {
          let key = i[0]
          let value = i[1]
          if ((mapB.get(key) ?? 0) < value) {
            return false
          }
        }
        return true
      }
    </script>
  </body>
</html>
